package com.hsingh.eclipse.mercury.extension.launchconfiguration;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.swt.graphics.Image;

import com.hsingh.eclipse.mercury.extensionpoint.definition.AbstractTask;
import com.hsingh.eclipse.mercury.extensionpoint.definition.ITask;

public class LaunchConfigurationTask extends AbstractTask implements ITask {

  private final ILaunchConfiguration config;
  private final String mode;

  public LaunchConfigurationTask(ILaunchConfiguration config, String mode) {
    this.config = config;
    this.mode = mode;
  }

  @Override
  public String getName() {
    if (ILaunchManager.RUN_MODE.equals(mode)) {
      return config.getName();
    } else if (ILaunchManager.DEBUG_MODE.equals(mode)) {
      return config.getName() + " - Debug";
    } else if (ILaunchManager.PROFILE_MODE.equals(mode)) {
      return config.getName() + " - Profile";
    } else {
      return config.getName() + " - " + mode;
    }
  }

  @Override
  public String getLabel() {
    return getName();
  }

  @Override
  public String getDetailsLabel() {
    return "Launch Configuration";
  }

  @Override
  public Object getItem() {
    return config;
  }

  @Override
  public Image getImage() {
    try {
      Image img = DebugUITools.getImage(config.getType().getIdentifier());
      return img;
    } catch (CoreException e) {
      return super.getImage();
    }
  }

  @Override
  public void execute(ExecutionEvent event) {
    DebugUITools.launch(config, mode);
  }
}
